package org.example.Forms;

import org.example.Controller.FormManagerNode;
import org.example.Tool.Component.CustomGraph;
import org.example.Tool.IRunFunc;
import org.example.Tool.UtilTool;
import org.example.dataHandle.Data;
import org.example.dataHandle.DataGroup;
import org.example.dataHandle.TaskRecord;


import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.Timer;


public class testForm extends AbstractForm{
    protected final DataGroup<Number> datagroup;

    protected TaskRecord<? extends Number> record;

    protected Timer timer;

    protected JPanel panel;

    protected HashMap<String,JLabel> presentation;

    protected final CustomGraph graph;

    protected BorderLayout layout;

    public testForm(FormManagerNode node) {
        super(node);
        datagroup = new DataGroup<>();
        graph = new CustomGraph(datagroup);
        componentInit();
        updateSystemInit();
    }

    public void setRecord(TaskRecord<? extends Number> record){
        this.record = record;
    }

    private void updateSystemInit(){
        timer = new Timer();
        if(!presentation.containsKey("历史最大值")){
            presentation.put("历史最大值",new JLabel());
            panel.add(presentation.get("历史最大值"));
        }
        if(!presentation.containsKey("历史最小值")){
            presentation.put("历史最小值",new JLabel());
            panel.add(presentation.get("历史最小值"));
        }
        if(!presentation.containsKey("历史平均值")){
            presentation.put("历史平均值",new JLabel());
            panel.add(presentation.get("历史平均值"));
        }
        if(!presentation.containsKey("已记录时长")){
            presentation.put("已记录时长",new JLabel());
            panel.add(presentation.get("已记录时长"));
        }
        setTimer(
                () -> {
                    if(record == null)
                        return;
                    Data.Pair<? extends Number, Long> Input;
                    Input = record.getLatest();
                    if(Input == null)
                        return;
                    synchronized (datagroup) {
                        datagroup.update(Input.getFirst(), Input.getSecond());
                    }
                    presentation.get("历史最大值").setText("历史最大值 :" + record.getMaxValue());
                    presentation.get("历史最小值").setText("历史最小值 :" + record.getMinValue());
                    presentation.get("历史平均值").setText("历史平均值 :" + String.format("%.2f",record.getAverageValue()));
                    presentation.get("已记录时长").setText("已记录时长 :" + UtilTool._24HTimingMethod((int)(record.getLatest().getSecond() / 1000)));
                    drawGraph();
                }
        );
    }



    protected void setTimer(IRunFunc inTask, long period){
        System.out.println("test: "+ Thread.currentThread());
        timer.scheduleAtFixedRate(
                new TimerTask() {
                    @Override
                public void run() {
                        Objects.requireNonNull(inTask);
                        inTask.run();
                    }
                },
                20, period);
    }
    protected void setTimer(IRunFunc inTask){
        this.setTimer(inTask,80);
    }

    protected void resetTimer(IRunFunc inTask, long period){
        timer.cancel();
        setTimer(inTask,period);
    }
    private void componentInit(){
        graph.setPreferredSize(new Dimension(800,600));
        panel = new JPanel();
        panel.setPreferredSize(new Dimension(200,600));
        layout = new BorderLayout(0,0);

        presentation = new HashMap<>();

        frame.setLocation(500,500);


        frame.setLayout(layout);
        frame.getContentPane().add(panel, BorderLayout.EAST);
        panel.setLayout(new FlowLayout(FlowLayout.LEFT,5,10));
        graph.setVisible(true);
        frame.getContentPane().add(graph, BorderLayout.CENTER);
        frame.pack();
    }

    @Override
    public void exit() {
        timer.cancel();
    }

    protected void drawGraph(){
        if(graph == null)
            throw new NullPointerException(this + ": Graph has not been initialized");
        graph.repaint();
    }
}
